﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Business.DataBusiness;
using System.Configuration;
using Business;
using System.Threading;

namespace MuaBanDT
{
    public partial class NhapKho : Form
    {
        Utilities getData = new Utilities();
        Form CTNhapKho;
        public NhapKho()
        {
            InitializeComponent();
        }
        private void NhapKho_Load(object sender, EventArgs e)
        {
            this.comboMaNhanVien.Items.AddRange(getData.GetMaNV("NK"));
            this.comboBoxMaDT.Items.AddRange(getData.GetDanhMucMaDT("DanhMucDT"));
            this.dataGridView1.DataSource = getData.GetDataTable("DanhMucDT");
            this.tBoxThueNhap.Text = "0";
            this.tBoxTongTien.Text = "0";
            this.tBoxKhuyenMai.Text = "0";
        }

        private void Xoa()
        {
            this.tBoxMaHDN.Text = "";
            this.tBoxDonViCungCap.Text = "";
            this.tBoxDiaChi.Text ="";
            this.comboMaNhanVien.Text ="";
            this.tBoxThueNhap.Text = "";
            this.tBoxTongTien.Text= "";
            this.tBoxKhuyenMai.Text= "";
            this.dateNgayNhap.Value = DateTime.Now;
        }
        private void dtXoaHDN_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void dtThoatHDN_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtCTHDN_Click(object sender, EventArgs e)
        {
            CTNhapKho = new ChiTietHoaDonNhap();
            CTNhapKho.Show();
        }

        private void dtLuuHDN_Click(object sender, EventArgs e)
        {
            try
            {
                HoaDonNhap a = new HoaDonNhap();
                a.MaHDN = getData.CreateID("HoaDonNhap","HDN");
                a.DonViCungCap = this.tBoxDonViCungCap.Text;
                a.DiaChi = this.tBoxDiaChi.Text;
                a.MaNV = this.comboMaNhanVien.Text;
                a.ThueNhap = float.Parse(this.tBoxThueNhap.Text);
                a.TongTien = float.Parse(this.tBoxTongTien.Text);
                a.KhuyenMai = float.Parse(this.tBoxKhuyenMai.Text);
                a.NgayNhap = DateTime.Parse(this.dateNgayNhap.Value.ToShortDateString());
                tBoxMaHDN.Text = a.MaHDN;
                if (a.Insert() == 1)
                {
                    MessageBox.Show("Lưu thành công", "Nhập Hàng");
                }
            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Nhập Hàng", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            
       
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.textboxTenDT.Text = getData.Value("DanhMucDT", e.RowIndex, 0);
                this.comboBoxMaDT.Text = getData.Value("DanhMucDT", e.RowIndex, 1);
                this.textBoxBangTan.Text = getData.Value("DanhMucDT", e.RowIndex, 2);
                this.textBoxTinhNang.Text = getData.Value("DanhMucDT", e.RowIndex, 3);
                this.textBoxBoNho.Text = getData.Value("DanhMucDT", e.RowIndex, 4);
                this.textBoxCongKN.Text = getData.Value("DanhMucDT", e.RowIndex, 5);
                this.textBoxSoLuong.Text = getData.Value("DanhMucDT", e.RowIndex, 6);
                this.textBoxThoiGianBH.Text = getData.Value("DanhMucDT", e.RowIndex, 7);
            }
            catch
            {
                this.textboxTenDT.Text = "";
                this.comboBoxMaDT.Text = "";
                this.textBoxBangTan.Text ="" ;
                this.textBoxTinhNang.Text ="" ;
                this.textBoxBoNho.Text ="";
                this.textBoxCongKN.Text = "";
                this.textBoxSoLuong.Text = "";
                this.textBoxThoiGianBH.Text = "";
            }
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonXoa_Click(object sender, EventArgs e)
        {
            this.textboxTenDT.Text = "";
            this.comboBoxMaDT.Text = "";
            this.textBoxBangTan.Text = "";
            this.textBoxTinhNang.Text = "";
            this.textBoxBoNho.Text = "";
            this.textBoxCongKN.Text = "";
            this.textBoxSoLuong.Text = "";
            this.textBoxThoiGianBH.Text = "";
        }

        private void buttonCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                MucDT a = new MucDT();
                a.TenDT = this.textboxTenDT.Text;
                a.MaDT = this.comboBoxMaDT.Text;
                a.BangTan = this.textBoxBangTan.Text;
                a.TinhNang = this.textBoxTinhNang.Text;
                a.BoNho = this.textBoxBoNho.Text;
                a.CongKetNoi = this.textBoxCongKN.Text;
                a.SoLuong = int.Parse(this.textBoxSoLuong.Text);
                a.ThoiGianBH = this.textBoxThoiGianBH.Text;
                a.Update();
                this.dataGridView1.DataSource = getData.GetDataTable("DanhMucDT");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
           
        }

        private void buttonXemCTHDN_Click(object sender, EventArgs e)
        {
            if (this.radioButtHoaDonNhap.Checked == true)
            {
                this.dataGridViewXemThongTin.DataSource = getData.GetDataTable("HoaDonNhap");
            }
            else
                this.dataGridViewXemThongTin.DataSource = getData.GetDataTable("ChiTietHDN");
        }

        private void tBoxThueNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            string thapPhanStr = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char thapPhanChar = Convert.ToChar(thapPhanStr);
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            { }
            else if (e.KeyChar == thapPhanChar && tBoxThueNhap.Text.IndexOf(thapPhanStr) == -1)
            { }
            else
            {
                MessageBox.Show("Bạn chỉ được nhập số", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void tBoxKhuyenMai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Bạn chỉ được nhập số", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void tBoxTongTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            tBoxKhuyenMai_KeyPress(sender, e);
        }

        private void textBoxSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            tBoxKhuyenMai_KeyPress(sender, e);
        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
            NhapKho_Load(sender, e);
            buttonCapNhat_Click(sender, e);
        }
     }
}
